package com.hospital.ssm.controller;

import com.hospital.ssm.model.Medicine;
import com.hospital.ssm.model.MedicineType;
import com.hospital.ssm.model.ResultInfo;
import com.hospital.ssm.service.MedicineService;
import com.hospital.ssm.service.MedicineTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLOutput;
import java.util.List;

/**
 * @author King
 * @create 2021-01-27-11:51
 */
@Controller
public class MedicineController {

    @Autowired
    private MedicineService medicineService;
    //药品类型
    @Autowired
    private MedicineTypeService medicineTypeService;
    // 查询全部药品信息数据
    @GetMapping("/medicineUi")
    public String medicineUi(Model model){
        try {
            List<Medicine> medicines = medicineService.queryAll();
            model.addAttribute("medicines",medicines);
            List<MedicineType>  medicineTypes= medicineTypeService.querymTypeAll();
            model.addAttribute("medicineTypes",medicineTypes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "medicine";
    }
    // 添加药品信息
    @PostMapping("/addmedUi")
    public String addmedUi(String mName,Double mPrice,String mmType,String mFactory,Integer mNumber){
        Medicine medicine = new Medicine(mName,mPrice,mmType,mFactory,mNumber);
        try {
            int i = medicineService.addMedicine(medicine);
            if ( i > 0 ){
                return "redirect:/medicineUi";
            }else{
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/medicineUi";
    }

    // 药品信息修改
    @ResponseBody
    @PutMapping("/updmedUi")
    public ResultInfo updmedUi(@RequestParam("mId") Integer mId, @RequestParam("mName") String mName, @RequestParam("mPrice") Double mPrice,
                           @RequestParam("mmType") String mmType, @RequestParam("mFactory") String mFactory,  @RequestParam("mNumber") Integer mNumber){
        ResultInfo info = new ResultInfo();
        Medicine medicine = new Medicine(mId,mName,mPrice,mmType,mFactory,mNumber);
        try {
            medicineService.updMedicine(medicine);
            info.setOk();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    //药品类型
    @GetMapping("/medicinetypeUi")
    public String medicinetypeUi(Model model){
        try {
            List<MedicineType>  medicineTypes= medicineTypeService.querymTypeAll();
            model.addAttribute("medicineTypes",medicineTypes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "medicinetype";
    }
}
